<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2016/8/29
 * Time: 10:58
 */

namespace app\models;


use app\librarys\Tools;

Class Card
{
    /******************卡片模板******************/
    /**
     * 获取卡片模板数量
     * @param $name 查询条件-名称
     * @return false|null|string
     */
    public static function getCardTplCount($name)
    {
        $sql = 'SELECT COUNT(*) FROM t_card_tpl WHERE name LIKE :name';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':name', '%' . $name . '%');

        $result = $cmd->queryScalar();
        return $result;
    }

    /**
     * 获取卡片模板list(分页)
     * @param $start 开始行
     * @param $pagesize 每页大小
     * @param $name 查询条件-名称
     * @return array
     */
    public static function getCardTplList($page = 1, $pagesize, $name)
    {
        //计算起始数据行
        if ($page <= 0) $page = 0;
        $start = ($page - 1) * $pagesize;

        $sql = 'SELECT t1.*,t2.name as create_manager_name FROM t_card_tpl t1 INNER JOIN t_manager t2 ON t2.id = t1.create_manager_id 
                WHERE t1.name LIKE :name ORDER BY t1.create_datetime DESC LIMIT :start,:pagesize';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':name', '%' . $name . '%');
        $cmd->bindValue(':start', $start);
        $cmd->bindValue(':pagesize', $pagesize);

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 获取卡片模板列表(不分页)
     * @return array
     */
    public static function getCardTpls()
    {
        $sql = 'SELECT id,name FROM t_card_tpl';
        $cmd = \Yii::$app->db->createCommand($sql);
        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 根据id获取卡片模板
     * @param $id
     * @return array|false
     */
    public static function getCardTplById($id)
    {
        $sql = 'SELECT * FROM t_card_tpl WHERE id = :id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);

        $result = $cmd->queryOne();
        if ($result == false) {
            return null;
        }
        return $result;
    }

    /**
     * 获取管理员机构所有商品
     * @param int $integral
     * @return array
     */
    public static function getCardTolAllGoods($integral = 0)
    {
        $sql = 'SELECT code,name,selling_price,NULL AS sort FROM t_goods 
                WHERE is_shelved = 1 AND is_enabled = 1 AND institution_code = \'0\'';

        if ($integral != 0) {
            $sql = $sql . ' AND selling_price <= :selling_price ';
        }

        $sql = $sql . ' ORDER BY code';

        $cmd = \Yii::$app->db->createCommand($sql);
        if ($integral != 0) {
            $cmd->bindValue(':selling_price', $integral);
        }
        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 根据卡片模板id获取产品
     * @param $id
     * @return array
     */
    public static function getCardTplGoodsById($id, $integral = 0)
    {
        $sql = 'SELECT t1.code,t1.name,t1.selling_price,t2.* FROM t_goods t1 
                  LEFT JOIN t_card_tpl_goods t2 ON t2.goods_code = t1.code AND t2.card_tpl_id=:id
                  LEFT JOIN t_card_tpl t3 ON t3.id = t2.card_tpl_id
                  WHERE t1.is_shelved = 1 AND t1.is_enabled = 1 AND t1.institution_code = \'0\'';

        if ($integral != 0) {
            $sql = $sql . ' AND t1.selling_price <= :selling_price ';
        }
        $sql = $sql . ' ORDER BY t2.sort DESC,t1.code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        if ($integral != 0) {
            $cmd->bindValue(':selling_price', $integral);
        }

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 修改/新增卡片模板
     * @param $id
     * @param $name
     * @param $description
     * @param $integral
     * @param $page_image_url
     * @param $create_manager_id
     * @return bool
     */
    public static function saveCardTpl($id, $name, $description, $integral, $page_image_url, $create_manager_id, $card_tpl_goods)
    {
        $connection = \Yii::$app->db;
        $transaction = $connection->beginTransaction();

        try {
            //保存卡片模板信息
            $cmd = \Yii::$app->db->createCommand('call p_save_card_tpl(:id,:name,:description,:integral,:page_image_url,:create_manager_id)');
            $cmd->bindValue(':id', $id);
            $cmd->bindValue(':name', $name);
            $cmd->bindValue(':description', $description);
            $cmd->bindValue(':integral', $integral);
            $cmd->bindValue(':page_image_url', $page_image_url);
            $cmd->bindValue(':create_manager_id', $create_manager_id);

            $card_tpl_id = $cmd->queryScalar();

            //保存卡片模板产品
            if ($card_tpl_id != '') {
                //清空卡片模板产品
                $sql = 'DELETE FROM t_card_tpl_goods WHERE card_tpl_id = :card_tpl_id';
                $cmd = \Yii::$app->db->createCommand($sql);
                $cmd->bindValue(':card_tpl_id', $card_tpl_id);
                $cmd->execute();

                //插入卡片模板产品
                foreach ($card_tpl_goods as $goods) {
                    $sql = 'INSERT INTO t_card_tpl_goods(card_tpl_id,goods_code,sort) VALUES (:card_tpl_id,:goods_code,:sort)';
                    $cmd = \Yii::$app->db->createCommand($sql);
                    $cmd->bindValue(':card_tpl_id', $card_tpl_id);
                    $cmd->bindValue(':goods_code', $goods->code);
                    $cmd->bindValue(':sort', $goods->sort);
                    $cmd->execute();
                }
            }

            $transaction->commit();
            $result = 1;
        } catch (\Exception $e) {
            $result = 0;
            $transaction->rollBack();
        }
        return $result > 0;
    }

    /**
     * 删除卡片模板
     * @param $id
     * @return bool
     */
    public static function delCardTpl($id)
    {
        $connection = \Yii::$app->db;
        $transaction = $connection->beginTransaction();
        try {
            $sql = 'DELETE FROM t_card_tpl WHERE id = :id';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':id', $id);

            $result = $cmd->execute();

            if ($result > 0) {
                $sql = 'DELETE FROM t_card_tpl_goods WHERE card_tpl_id = :card_tpl_id';
                $cmd = \Yii::$app->db->createCommand($sql);
                $cmd->bindValue(':card_tpl_id', $id);

                $result = $cmd->execute();
            }

            $result = 1;
            $transaction->commit();
        } catch (\Exception $e) {
            $result = 0;
            $transaction->rollBack();
        }


        return $result > 0;
    }

    /******************卡片******************/

    /**获取卡片数量
     * @param $card_type
     * @param $current_user_id
     * @param $current_institution_code
     * @param $name
     * @param $integral
     * @param $from_institution_code
     * @param $to_institution_code
     * @param $startTime
     * @param $endTime
     * @return false|null|string
     */
    public static function getCardCount($card_type, $current_user_id, $current_institution_code, $name, $voucher, $from_institution_code, $to_institution_code, $startTime, $endTime)
    {
        $sql = '';
        //管理员机构查看所有机构发放卡
        if ($current_user_id == '0') {
            $sql = 'SELECT count(*) FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE 1 = 1';
        } else {
            if ($card_type == 'send') { //发卡
                $from_institution_code = $current_institution_code;
                $sql = 'SELECT count(*) FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE t1.from_institution_code = :from_institution_code';
            } else if ($card_type == 'receive') { //收卡
                $to_institution_code = $current_institution_code;
                $sql = 'SELECT count(*) FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE t1.to_institution_code = :to_institution_code';
            }
        }

        if ($name != '') {
            $sql = $sql . ' AND t1.name Like :name';
        }
        if ($voucher != '') {
            $sql = $sql . ' AND t1.start_index <= :voucher AND t1.end_index >= :voucher';
        }

        if ($current_institution_code == '0') {
            if ($from_institution_code != '') {
                $sql = $sql . ' AND t1.from_institution_code = :from_institution_code';
            }
        }

        if ($current_institution_code == '0' || $card_type == 'send') {
            if ($to_institution_code != '') {
                $sql = $sql . ' AND t1.to_institution_code = :to_institution_code';
            }
        }

        if ($startTime != '') {
            $sql = $sql . ' AND t1.create_datetime >= :startTime';
        }
        if ($endTime != '') {
            $sql = $sql . ' AND t1.create_datetime <= :endTime';
        }


        $cmd = \Yii::$app->db->createCommand($sql);
        if ($name != '') {
            $cmd->bindValue(':name', '%' . $name . '%');
        }
        if ($voucher != '') {
            $cmd->bindValue(':voucher', $voucher);
        }


        if ($current_institution_code == '0' || $card_type == 'send') {
            if ($from_institution_code != '') {
                $cmd->bindValue(':from_institution_code', $from_institution_code);
            }
        }

        if ($current_institution_code == '0' || $card_type == 'send' || $card_type == 'receive') {
            if ($to_institution_code != '') {
                $cmd->bindValue(':to_institution_code', $to_institution_code);
            }
        }

        if ($startTime != '') {
            $cmd->bindValue(':startTime', $startTime);
        }
        if ($endTime != '') {
            $cmd->bindValue(':endTime', $endTime);
        }


        $result = $cmd->queryScalar();
        return $result;
    }

    /**
     * 获取发卡记录
     * @param $page
     * @param $pagesize
     * @param $card_type
     * @param $current_user_id
     * @param $current_institution_code
     * @param $name
     * @param $voucher
     * @param $from_institution_code
     * @param $to_institution_code
     * @param $startTime
     * @param $endTime
     * @return array
     */
    public static function getCardList($page, $pagesize, $card_type, $current_user_id, $current_institution_code, $name, $voucher, $from_institution_code, $to_institution_code, $startTime, $endTime)
    {
        $sql = '';
        //管理员机构查看所有机构发放卡
        if ($current_user_id == '0') {
            $sql = 'SELECT t1.*,t2.name as from_institution_name,t3.name as to_institution_name,t3.parent_code as to_institution_parent_code,t4.name as create_manager_name FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE 1 = 1';
        } else {
            if ($card_type == 'send') { //发卡
                $from_institution_code = $current_institution_code;
                $sql = 'SELECT t1.*,t2.name as from_institution_name,t3.name as to_institution_name,t3.parent_code as to_institution_parent_code,t4.name as create_manager_name FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE t1.from_institution_code = :from_institution_code';
            } else if ($card_type == 'receive') { //收卡
                $to_institution_code = $current_institution_code;
                $sql = 'SELECT t1.*,t2.name as from_institution_name,t3.name as to_institution_name,t3.parent_code as to_institution_parent_code,t4.name as create_manager_name FROM t_card t1
                      LEFT JOIN t_institution t2 ON  t2.code = t1.from_institution_code
                      INNER JOIN t_institution t3 ON t3.code = t1.to_institution_code
                      INNER JOIN t_manager t4 ON t4.id = t1.`create_manager_id`
                      WHERE t1.to_institution_code = :to_institution_code';
            }
        }

        if ($name != '') {
            $sql = $sql . ' AND t1.name Like :name';
        }
        if ($voucher != '') {
            $sql = $sql . ' AND t1.start_index <= :voucher AND t1.end_index >= :voucher';
        }

        if ($current_institution_code == '0') {
            if ($from_institution_code != '') {
                $sql = $sql . ' AND t1.from_institution_code = :from_institution_code';
            }
        }

        if ($current_institution_code == '0' || $card_type == 'send') {
            if ($to_institution_code != '') {
                $sql = $sql . ' AND t1.to_institution_code = :to_institution_code';
            }
        }

        if ($startTime != '') {
            $sql = $sql . ' AND t1.create_datetime >= :startTime';
        }
        if ($endTime != '') {
            $sql = $sql . ' AND t1.create_datetime <= :endTime';
        }

        $sql = $sql . ' ORDER BY create_datetime DESC LIMIT :start,:pagesize';

        $cmd = \Yii::$app->db->createCommand($sql);
        if ($name != '') {
            $cmd->bindValue(':name', '%' . $name . '%');
        }
        if ($voucher != '') {
            $cmd->bindValue(':voucher', $voucher);
        }

        if ($current_institution_code == '0' || $card_type == 'send') {
            if ($from_institution_code != '') {
                $cmd->bindValue(':from_institution_code', $from_institution_code);
            }
        }

        if ($current_institution_code == '0' || $card_type == 'send' || $card_type == 'receive') {
            if ($to_institution_code != '') {
                $cmd->bindValue(':to_institution_code', $to_institution_code);
            }
        }

        if ($startTime != '') {
            $cmd->bindValue(':startTime', $startTime);
        }
        if ($endTime != '') {
            $cmd->bindValue(':endTime', $endTime);
        }


        //计算起始数据行
        if ($page <= 0) $page = 0;
        $start = ($page - 1) * $pagesize;

        $cmd->bindValue(':start', $start);
        $cmd->bindValue(':pagesize', $pagesize);

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 根据收卡机构编码获取卡片列表
     * @param $institution_code
     * @return array
     */
    public static function getCardsByInstitutionCode($institution_code)
    {
        $sql = 'SELECT * from t_card WHERE to_institution_code = :to_institution_code ORDER BY create_datetime DESC';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':to_institution_code', $institution_code);
        $result = $cmd->queryAll();

        return $result;
    }

    /**
     * 根据卡片id获取产品
     * @param $id
     * @return array
     */
    public static function getCardGoodsById($id)
    {
        $sql = 'SELECT t1.code,t1.name,t1.selling_price,t2.* FROM t_goods t1 
                  LEFT JOIN t_card_goods t2 ON t2.goods_code = t1.code AND t2.card_id=:id
                  LEFT JOIN t_card t3 ON t3.id = t2.card_id
                  WHERE t1.is_shelved = 1 AND t1.is_enabled = 1 AND t1.institution_code = \'0\'
                  ORDER BY t2.sort DESC,t1.code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);

        $result = $cmd->queryAll();
        return $result;
    }

    public static function getCardSelectedGoodsById($id)
    {
        $sql = 'SELECT t1.code,t1.name,t1.selling_price,t2.* FROM t_goods t1 
                  LEFT JOIN t_card_goods t2 ON t2.goods_code = t1.code AND t2.card_id=:id
                  LEFT JOIN t_card t3 ON t3.id = t2.card_id
                  WHERE t1.is_shelved = 1 AND t1.is_enabled = 1 AND t1.institution_code = \'0\' AND t2.card_id=:id
                  ORDER BY t2.sort DESC,t1.code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);

        $result = $cmd->queryAll();
        return $result;
    }

    public static function getCardGoodsByIdAndIntegral($id, $integral)
    {
        $sql = 'SELECT t1.code,t1.name,t1.selling_price,t2.* FROM t_goods t1 
                  LEFT JOIN t_card_goods t2 ON t2.goods_code = t1.code AND t2.card_id=:id
                  LEFT JOIN t_card t3 ON t3.id = t2.card_id
                  WHERE t1.is_shelved = 1 AND t1.is_enabled = 1 AND t1.institution_code = \'0\' AND (t1.selling_price <= :integral OR t2.card_id IS NOT NULL)
                  ORDER BY t2.sort DESC,t1.code';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $cmd->bindValue(':integral', $integral);

        $result = $cmd->queryAll();
        return $result;
    }

    /**
     * 修改卡片信息
     * @param $id
     * @param $name
     * @param $description
     * @param $start_index
     * @param $end_index
     * @param $from_institution_code
     * @param $to_institution_code
     * @param $integral
     * @param $create_manager_id
     * @param $goodslist
     * @return bool
     */
    public static function saveCard($isSystemAdmin, $id, $name, $description, $start_index, $end_index, $from_institution_code,
                                    $to_institution_code, $integral, $create_manager_id, $goodslist)
    {
        $connection = \Yii::$app->db;
        $transaction = $connection->beginTransaction();


        //判断单证号是否为为发放状态
        if ($id == '') {
            $checkResult = Voucher::checkVoucher($isSystemAdmin,$start_index, $end_index);
            if ($checkResult['status'] == 1) {
                return $checkResult;
            }
        }

        //判断被发放的机构是否包含商品
        foreach ($goodslist as $goods) {
            $sql = 'SELECT 1 FROM t_goods WHERE institution_code = :institution_code AND code = :code';
            $cmd = \Yii::$app->db->createCommand($sql);
            $cmd->bindValue(':institution_code', $to_institution_code);
            $cmd->bindValue(':code', $goods['code']);
            $result = $cmd->queryOne();
            if ($result == false) {
                return ['status' => 1, 'message' => '下级机构无该商品，商品编码为：' . $goods['code']];
            }
        }


        try {
            if($id == '') {
                if ($create_manager_id == '0') {
                    //修改单证状态
                    Voucher::updateVoucherStatus($start_index, $end_index, 1); // 0-未使用，1-已绑定，2-已激活，3-已使用，4-失效，5-过期，6-二级机构绑定
                } else {
                    Voucher::updateVoucherStatus($start_index, $end_index, 6);
                }
            }


            //保存卡片模板信息
            $cmd = \Yii::$app->db->createCommand('call p_save_card(:id,:name,:description,:start_index,:end_index,:from_institution_code,
                :to_institution_code,:integral,:create_manager_id)');
            $cmd->bindValue(':id', $id);
            $cmd->bindValue(':name', $name);
            $cmd->bindValue(':description', $description);
            $cmd->bindValue(':start_index', $start_index);
            $cmd->bindValue(':end_index', $end_index);
            $cmd->bindValue(':from_institution_code', $from_institution_code);
            $cmd->bindValue(':to_institution_code', $to_institution_code);
            $cmd->bindValue(':integral', $integral);
            $cmd->bindValue(':create_manager_id', $create_manager_id);

            $card_id = $cmd->queryScalar();

            //保存卡片产品
            if ($card_id != '') {

                //插入卡片对应的单证表
                for ($i = $start_index; $i <= $end_index; $i++) {
                    $sql = 'INSERT INTO t_card_voucher(card_id, voucher_id) VALUES (:card_id, :voucher_id)';
                    $cmd = \Yii::$app->db->createCommand($sql);
                    $cmd->bindValue(':card_id', $card_id);
                    $cmd->bindValue(':voucher_id', $i);
                    $cmd->execute();
                }


                //清空卡片产品
                $sql = 'DELETE FROM t_card_goods WHERE card_id = :card_id';
                $cmd = \Yii::$app->db->createCommand($sql);
                $cmd->bindValue(':card_id', $card_id);
                $cmd->execute();

                //插入卡片模板产品
                foreach ($goodslist as $goods) {
                    $sql = 'INSERT INTO t_card_goods(card_id,goods_code,sort) VALUES (:card_id,:goods_code,:sort)';
                    $cmd = \Yii::$app->db->createCommand($sql);
                    $cmd->bindValue(':card_id', $card_id);
                    $cmd->bindValue(':goods_code', $goods['code']);
                    $cmd->bindValue(':sort', $goods['sort']);
                    $cmd->execute();
                }
            }

            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            return ['status' => 1, 'message' => '服务器错误'];
        }

        return ['status' => 0, 'message' => '执行成功'];
    }

    /**
     * 根据id获取卡片信息
     * @param $id
     * @return array|false|null
     */
    public static function getCardById($id)
    {
        $sql = 'SELECT *,\'\' AS from_institution_name,\'\' AS to_institution_name FROM t_card WHERE id=:id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':id', $id);
        $result = $cmd->queryOne();
        if ($result == false) {
            return null;
        }
        return $result;
    }

    /**
     * 根据传入的发放机构code和它卡片的起始单证号获取最后一次发放记录
     * @param $from_institution_code
     * @param $start_index
     * @param $end_index
     * @return array|false|null
     */
    public static function getLastCardSend($from_institution_code, $start_index, $end_index)
    {
        $sql = 'SELECT * FROM t_card WHERE from_institution_code = :from_institution_code
                         AND start_index >= :start_index
                         AND end_index <= :end_index
                         ORDER BY end_index DESC';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':from_institution_code', $from_institution_code);
        $cmd->bindValue(':start_index', $start_index);
        $cmd->bindValue(':end_index', $end_index);

        $result = $cmd->queryOne();
        if ($result == false) {
            return null;
        }
        return $result;
    }

    /**
     * 根据卡号id获取单证列表
     * @param $card_id
     * @return array
     */
    public static function getVoucherByCardId($card_id)
    {
        $sql = 'SELECT t2.*,t3.create_datetime,t4.name,t4.phone from t_card t1
                  INNER JOIN t_voucher t2 ON t2.id >= t1.start_index AND t2.id <= t1.end_index
                  LEFT JOIN t_card_voucher_record t3 ON t3.voucher_id = t2.voucher
                  LEFT JOIN t_user t4 on t4.id = t3.user_id
                WHERE t1.id = :card_id';
        $cmd = \Yii::$app->db->createCommand($sql);
        $cmd->bindValue(':card_id', $card_id);

        $result = $cmd->queryAll();
        return $result;
    }


    /**
     * 根据卡片id激活卡片内单证
     * @param $card_id
     * @return array
     */
    public static function activateCardVoucher($card_id)
    {
        $vouchers = self::getVoucherByCardId($card_id);
        if ($vouchers == null) {
            return ['status' => 1, 'message' => '该卡片无单证'];
        }
        foreach ($vouchers as $voucher) {
            if ($voucher['status'] == 0 || $voucher['status'] == 1 || $voucher['status'] == 6) {
                $result = Voucher::activateVoucher($voucher['id']);
                if ($result['status'] == 1) {
                    return $result;
                }
            }
        }
        return ['status' => 0, 'message' => '执行成功'];
    }

    /**
     * 根据用户微信openid来获取此用户所有的卡片
     * @param $wxOpenid
     * @return array
     */
    public static function getCardListByWeixinOpenid($wxOpenid)
    {
        $cmd = \Yii::$app->db->createCommand('
            SELECT t2.`name`, t0.create_datetime, t4.`code` AS institution_code, t4.`name` AS institution_name FROM t_card_voucher_record t0
            LEFT JOIN t_card_voucher t1 ON t1.voucher_id=t0.voucher_id
            LEFT JOIN t_card t2 ON t2.id=t1.card_id
            LEFT JOIN t_user t3 ON t3.id=t0.user_id
            LEFT JOIN t_institution t4 ON t4.`code`=t2.to_institution_code
            WHERE t3.wx_openid=:wxOpenid AND t3.is_enabled=1
            ORDER BY t0.create_datetime DESC');
        $cmd->bindValue(':wxOpenid', $wxOpenid);

        $items = $cmd->queryAll();
        return $items;
    }
}